package com.zzjson.thread;

import java.util.concurrent.TimeUnit;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : 通过 jps查看进程，然后jstack查看线程堆栈日志，使用线程一定要定义名称</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年08月09日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class ThreadStatusDemo {
	public static void main(String[] args) {

		/**
		 * "Time_Waiting_Thread" #11 prio=5 os_prio=31 tid=0x00007fcc4b061000 nid=0xa803 waiting on condition
		 * [0x000070000a7e0000]
		 *    java.lang.Thread.State: TIMED_WAITING (sleeping)
		 *         at java.lang.Thread.sleep(Native Method)
		 *         at java.lang.Thread.sleep(Thread.java:340)
		 *         at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
		 *         at com.zzjson.thread.ThreadStatusDemo.lambda$main$0(ThreadStatusDemo.java:33)
		 *         at com.zzjson.thread.ThreadStatusDemo$$Lambda$1/1329552164.run(Unknown Source)
		 *         at java.lang.Thread.run(Thread.java:748)
		 */
		new Thread(() -> {
			while (true) {
				try {
					TimeUnit.SECONDS.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}, "Time_Waiting_Thread").start();

		/**
		 * "waiting_Thread" #12 prio=5 os_prio=31 tid=0x00007fa7c1911800 nid=0xa803 in Object.wait() [0x0000700008512000]
		 *    java.lang.Thread.State: WAITING (on object monitor)
		 *         at java.lang.Object.wait(Native Method)
		 *         - waiting on <0x000000076ac82790> (a java.lang.Class for com.zzjson.thread.ThreadStatusDemo)
		 *         at java.lang.Object.wait(Object.java:502)
		 *         at com.zzjson.thread.ThreadStatusDemo.lambda$main$1(ThreadStatusDemo.java:57)
		 *         - locked <0x000000076ac82790> (a java.lang.Class for com.zzjson.thread.ThreadStatusDemo)
		 *         at com.zzjson.thread.ThreadStatusDemo$$Lambda$2/1791741888.run(Unknown Source)
		 *         at java.lang.Thread.run(Thread.java:748)
		 */
		new Thread(() -> {
			while (true) {
				synchronized (ThreadStatusDemo.class) {
					//凡是带阻塞的都会抛 InterruptedException
					try {
						ThreadStatusDemo.class.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}, "waiting_Thread").start();

		/**
		 *
		 * 获得锁之后睡眠（sleep），就是time_waiting
		 * "Blocked01_Thread" #14 prio=5 os_prio=31 tid=0x00007fcc4a0ac800 nid=0x5603 waiting on condition
		 * [0x000070000a8e3000]
		 *    java.lang.Thread.State: TIMED_WAITING (sleeping)
		 *         at java.lang.Thread.sleep(Native Method)
		 *         at java.lang.Thread.sleep(Thread.java:340)
		 *         at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
		 *         at com.zzjson.thread.ThreadStatusDemo$BlockedDemo.run(ThreadStatusDemo.java:97)
		 *         - locked <0x000000076b0f2708> (a java.lang.Class for com.zzjson.thread.ThreadStatusDemo$BlockedDemo)
		 *         at java.lang.Thread.run(Thread.java:748)
		 */

		new Thread(new BlockedDemo(), "Blocked01_Thread").start();
		/**
		 *
		 * "Blocked02_Thread" #16 prio=5 os_prio=31 tid=0x00007fa7be881000 nid=0xa503 waiting
		 * for monitor entry [0x0000700008718000]
		 *    java.lang.Thread.State: BLOCKED (on object monitor)
		 *         at com.zzjson.thread.ThreadStatusDemo$BlockedDemo.run(ThreadStatusDemo.java:97)
		 *         - waiting to lock <0x000000076b0f2720> (a java.lang.Class for com.zzjson.thread
		 *         .ThreadStatusDemo$BlockedDemo)
		 *         at java.lang.Thread.run(Thread.java:748)
		 */

		new Thread(new BlockedDemo(), "Blocked02_Thread").start();

	}

	//两个线程争抢锁，一个线程获取锁，另外一个线程是阻塞状态
	static class BlockedDemo extends Thread {
		@Override
		public void run() {
			synchronized (BlockedDemo.class) {
				while (true) {
					try {
						TimeUnit.SECONDS.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

				}
			}
		}
	}

}